import requests
import time
import re

class Coin55(object):
    def __init__(self, page):
        self.page = page
        self.news_father_url = "http://www.55coin.com/article/"
        self.api_url = "http://www.55coin.com/index/article/search.html?cat_id=4&page={}".format(self.page)

    def get_response(self):
        json_data = requests.get(self.api_url, headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36"
        })
        return json_data.json()['list']

    def data_dispose(self, api_response):
        data = []
        for i in api_response:
            data_dict = {}

            content_id = i["article_id"]
            content = self.get_content(content_id)

            data_dict["id"] = content_id
            data_dict["create_time"] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(i["add_time"]))
            data_dict["update_time"] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(i["last_time"]))
            data_dict["title"] = i["title"]
            data_dict["source"] = i["nickname"]
            data_dict["digest"] = i["brief"]
            data_dict["content"] = content
            data_dict["clicks"] = i["show_total"]
            data_dict["index_image_url"] = i["rectangle_img"]
            data_dict["category_id"] = i["cat_id"]
            data_dict["user_id"] = 3
            data.append(data_dict)
        return data

    def get_content(self, id):
        news_url = self.news_father_url + str(id) + ".html"
        response = requests.get(news_url, headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36"
        }).content.decode()
        response = re.sub("\n","", response)
        response = re.sub("\r", "", response)
        response = re.sub("\t", "", response)
        response = re.sub(" ", "", response)
        content = re.findall(r'<articleclass="article-content"style="min-height:500px;">(.*?)</article>', response)[0]
        content = re.sub('区势传媒', '链檬资讯', content)
        content = re.sub('区势传媒是一家“研究型媒体”，专注于区块链经济和数字货币理论研究，旨在让一部分人先看懂“区势”。更多区块链信息请关注区势传媒www.55coin.com，项目推广、商务合作请联系微信号：qscm55coin。', '', content)
        return content

    def coin55_spider(self):
        api_response = self.get_response()
        return self.data_dispose(api_response)


if __name__ == '__main__':
    coin55 = Coin55(1)
    coin55.coin55_spider()